package han.chensing.latos.common.level.content.timeLine.motion.interpolator;

import java.io.Serial;
import java.io.Serializable;

import han.chensing.latos.common.level.util.MathUtil;

public class Bezier2Interpolator implements Interpolator, Serializable {

    public static final float defaultAccuracy=4E-4F;
    @Serial
    private static final long serialVersionUID = 1512492694394959803L;

    /**
     * Each ranges from 0-1
     */
    protected float x1,y1,x2,y2;

    //P0 (1-t)^(3)+3 t P2 (1-t)^(2)+3 t^(2) P3 (1-t)+t^(3) P1
    protected final float p0x=0,p0y=0,p3x=1,p3y=1;
    protected float accuracy=defaultAccuracy;

    @Override
    public float value(float x) {
        return MathUtil.getBezierAbsolute(
                x,p0x,p0y,x1,y1,x2,y2,p3x,p3y,0,1,
                accuracy);
    }

    @Override
    public float integration(float x) {
        return MathUtil.getBezierIntegration(p0x,p0y,x1,y1,x2,y2,p3x,p3y);
    }

    public float getAccuracy() {
        return accuracy;
    }

    public void setAccuracy(float accuracy) {
        this.accuracy = accuracy;
    }

    public float getX1() {
        return x1;
    }

    public void setX1(float x1) {
        this.x1 = x1;
    }

    public float getY1() {
        return y1;
    }

    public void setY1(float y1) {
        this.y1 = y1;
    }

    public float getX2() {
        return x2;
    }

    public void setX2(float x2) {
        this.x2 = x2;
    }

    public float getY2() {
        return y2;
    }

    public void setY2(float y2) {
        this.y2 = y2;
    }

    public void setPosition(float x1,float y1,float x2,float y2){
        this.x1=x1;
        this.x2=x2;
        this.y1=y1;
        this.y2=y2;
    }

    public Bezier2Interpolator(){

    }

    public Bezier2Interpolator(float x1, float y1, float x2, float y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }
}
